﻿using DXBase;
using DXBase.Async;
using DXBase.D3D;
using DXBase.Samples;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Sample1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
		public MainPage()
		{
			this.InitializeComponent();
			Load();
		}
		InputController input;

		async void Load()
		{
			input = new InputController() { CoreWindow = CoreWindow.GetForCurrentThread() };
			Universes = new ObservableCollection<Universe>();

			Loading = true;
			await DXBase.Samples.Universes.CreateMultiverse(Universes, scenePanel.Context, input);
			Current = Universes.FirstOrDefault();
			Loading = false;
		}

		public bool Loading
		{
			get { return (bool)GetValue(LoadingProperty); }
			set { SetValue(LoadingProperty, value); }
		}

		public static readonly DependencyProperty LoadingProperty = DependencyProperty.Register(
			"Loading", 
			typeof(bool), 
			typeof(MainPage), 
			new PropertyMetadata(false));

		public Universe Current
		{
			get { return (Universe)GetValue(CurrentProperty); }
			set { SetValue(CurrentProperty, value); }
		}
		public static readonly DependencyProperty CurrentProperty = DependencyProperty.Register("Current", typeof(Universe), typeof(MainPage), new PropertyMetadata(null));

		public ObservableCollection<Universe> Universes
		{
			get { return (ObservableCollection<Universe>)GetValue(UniversesProperty); }
			set { SetValue(UniversesProperty, value); }
		}
		public static readonly DependencyProperty UniversesProperty = DependencyProperty.Register("Universes", typeof(ObservableCollection<Universe>), typeof(MainPage), new PropertyMetadata(null));

		private async void DoSave(object sender, RoutedEventArgs e) 
		{
			await scenePanel.Context.SavePic(lockSavePic); 
		}
		private void DoReset(object sender, RoutedEventArgs e) 
		{
			if (Current != null)
				Current.Reset();
		}

		AsyncLock lockSavePic = new AsyncLock();
	}
}
